from typing import Optional, TypeVar, Any, Generic

from pydantic import BaseModel

from app.common.error_code import ErrorCode

T = TypeVar("T")


class Result(BaseModel, Generic[T]):
    """
    请求响应结果
    """
    success: bool
    code: int
    message: str
    data: Optional[T]

    @classmethod
    def ok(cls, data=None) -> "Result":
        return Result(success=True, code=0, message="", data=data)

    @classmethod
    def fail(cls, message: str, code=-1) -> "Result":
        return Result(success=False, code=code, message=message, data=None)

    @classmethod
    def fail_with_error_code(cls, error_code: ErrorCode) -> "Result":
        return Result(success=False, code=error_code.code, message=error_code.msg, data=None)




